/**************************************************
|	File:		CProjectile.cpp
|	Author:		Benjamin Budian
|	Course:		SGD
|	Purpose:	Projectile is an arrow fired from an entity
**************************************************/
#include "Projectile.h"

//Wrappers
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "../SGD Wrappers/CSGD_MessageSystem.h"
#include "../SGD Wrappers/CSGD_EventSystem.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "../SGD Wrappers/SGD_Math.h"
#include "../SGD Wrappers/SGD_String.h"

//Locals
#include "DestroyEntityMessage.h"
#include "GameplayState.h"
#include "Game.h"

CProjectile::CProjectile(int nFrameID)
{
	m_fDamage		= 50;
	m_nNumCols		= 8;
	m_nFrameWidth	= 64;
	m_nFrameHeight	= 64;
	m_nCurrentFrame = nFrameID;

	SetWidth(64);
	SetHeight(64);

	m_pOwner = nullptr;

	m_eType = ENT_PROJECTILE;

	int nImageID = CSGD_TextureManager::GetInstance()->LoadTexture( _T("resource/images/Player/Projectiles/bb_projectiles_resized.png"));
	SetImageID(nImageID);

	m_bDebug = false;

	CSGD_EventSystem::GetInstance()->RegisterClient("DEBUG",this);
}

CProjectile::~CProjectile(void)
{
	CSGD_EventSystem::GetInstance()->UnregisterClientAll(this);
	m_pOwner->Release();
	m_pOwner = nullptr;
}

void CProjectile::SetOwner(CEntity* pOwner){
	
	if(m_pOwner != nullptr)
		m_pOwner->Release();
	
	m_pOwner = pOwner;

	if(m_pOwner != nullptr)
		m_pOwner->AddRef();
}

void CProjectile::Update( float fElapsedTime ){ 

	CEntity::Update(fElapsedTime);

	if(GetPosX() <= 0 || GetPosX() >= CGame::GetInstance()->GetScreenWidth() || GetPosY() <= 0 || GetPosY() >= CGame::GetInstance()->GetScreenHeight()){
		CDestroyEntityMessage* pMsg = new CDestroyEntityMessage( this );
		CSGD_MessageSystem::GetInstance()->SendMsg( pMsg );
		pMsg = nullptr;
	}
}	

void CProjectile::Render( void ){

	if(m_bDebug){
		RECT rCollisionBox = GetRect();
		CSGD_Direct3D::GetInstance()->DrawRect(rCollisionBox, D3DCOLOR_ARGB(127,255,255,0));

		TOSTRINGSTREAM toss;

		toss << "(" << GetPosX() << ", " << GetPosY() << ")\n"
			<< "Projectile Type: " << GetType() << '\n'
			<< "Damage: " << m_fDamage << '\n';

		CBitmapFont* pFont = CGame::GetInstance()->GetFont();

		pFont->Draw(toss.str().c_str(),(int)(GetPosX() + GetWidth()), (int)(GetPosY()), .34f,D3DCOLOR_ARGB(255,0,0,0));

	}
  	RECT rFrame = CellAlgorithm(m_nCurrentFrame);

	CSGD_TextureManager::GetInstance()->Draw(GetImageID(),(int)GetPosX(),(int)GetPosY(),1.0f,1.0f,&rFrame);
}

void CProjectile::HandleCollision( const IEntity* pOther ){ 

	if(m_pOwner != pOther){
		CSGD_EventSystem::GetInstance()->SendEvent("ENTITY_HIT",nullptr,(CEntity*)(pOther), this);

		CDestroyEntityMessage* pMsg = new CDestroyEntityMessage( this );

		CSGD_MessageSystem::GetInstance()->SendMsg( pMsg );
		pMsg = nullptr;
	}

}	

RECT CProjectile::CellAlgorithm(int ID){
	RECT rCell = {};

 	rCell.left = (ID % m_nNumCols) * m_nFrameWidth;
	rCell.top = (ID / m_nNumCols) * m_nFrameHeight;

	rCell.right = rCell.left + m_nFrameWidth;
	rCell.bottom = rCell.top + m_nFrameHeight;

	return rCell;
}

void CProjectile::HandleEvent( CEvent* pEvent ){
	if(pEvent->GetEventID() == "DEBUG")
		m_bDebug = !m_bDebug;
}




